/**
 * Created by zhangxinhai on 2017/4/18.
 */

angular.module('myApp')

    .controller('queryCtrl',['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'dateFilter','myConstant','DateCalculateService', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                                                     CacheService, limitToFilter, filterFilter, dateFilter,myConstant,DateCalculateService) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            vm.downCfg = {
                contentType : 'jqLike',
                date : {
                    'sumDate' : 'yyyyMMdd'
                }
            }
            //变量初始化
            //查询条件
            vm.queryBean = {};
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            vm.queryBean.transConfirmDate = DateCalculateService.getToday();

            //缓存数据初始化(需要缓存的key请自定义)
            /*数据格式{
             key1 :value1,
             key2:value2
             }*/
            vm.cached = {
                TRANS_CODE_ALL:{},
                COMANY_CODE: {}
            };

            CacheService.initCache(vm.cached);

            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            //获取对象数据[user,user,user]
            vm.getObj = function (key, input) {
                return $http.post(config.ctx + '/base/cache/getAll',
                    $httpParamSerializerJQLike({cacheKey: key}))
                    .then(function (response) {
                        return limitToFilter(filterFilter(response.data.object, input), 6);
                    }, function (response) {
                        $log.error('获取数据%s失败', cacheKey);
                    })
            };

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;
            vm.searchFailure = searchFailure;

            /*------------------以上配置通用---------------------*/


            /*------------------以上方法名可选择性通用---------------------*/

            /*CacheService.initCache(vm.cached, function (cacheKey, cacheObj) {
             $log.debug(cacheKey, cacheObj);
             vm.cached[cacheKey] = cacheObj;
             });*/

            /*vm.getCache = function (key) {
             CacheService.getCache(key)
             }*/

            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');
                $http.post(vm.url, vm.queryBean, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            console.info(data.object);
                            vm.pagination.list = data.object;
                            vm.sumObject = data.sumObject;
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert("操作失败");
                    });

            };

            function resetForm() {
                vm.queryBean = {};
            }

            function searchFailure(bean) {
                vm.url = "searchFailure";
                vm.pagination.queryBean = angular.copy(bean);
                $http.post(vm.url, vm.pagination,config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            OpenService({
                                modalTitle: '支付指令失败明细',
                                modalBody: 'initFailure',
                                url: 'queryDetail',
                                item: {
                                    bean:bean
                                },
                                pagination:data.object,
                                cached: {
                                    COMANY_CODE: vm.cached.COMANY_CODE,
                                    TRANS_CODE_ALL: vm.cached.TRANS_CODE_ALL
                                }
                            }, function (item) {
                                $log.debug(item);
                                vm.queryDetail();
                            }, 'searchFailure','myModalNoFooter.html');
                        } else {
                            alert('未找到相关数据');
                            return false;
                        }
                    });
            }
        }]);

angular.module('myApp').controller('searchFailure',['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', '$log', function ($scope, $uibModalInstance, modalItem, $http,
                                                    $httpParamSerializerJQLike, $log) {

        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {},
            clrFlag: ''
        }
        //分页数据
        vm.pagination = {
            pageSize: 10,
            pageNum: 1
        };
        vm = $scope.vm = modalItem;
        vm.queryDetail = queryDetail;

        function queryDetail() {
            vm.url = "searchFailure";
            var postData = {};
            postData.queryBean = angular.copy(vm.item.bean);
            postData.pageSize = vm.pagination.pageSize;
            postData.pageNum = vm.pagination.pageNum;
            $http.post(vm.url, postData, config.jsonHeader)
                .then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        vm.pagination = data.object;
                    } else {
                        alert('未找到相关数据');
                        return false;
                    }
                });
        }

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    }]);
